#ifndef RSSCONTAINER_H
#define RSSCONTAINER_H

//includes
#include <QObject>
#include <QList>
#include <QString>
#include <QHttp>
#include <QHttpResponseHeader>
#include <QXmlSimpleReader>
#include <QXmlSimpleReader>
#include <QXmlDefaultHandler> 

#include "rsschannel.h"
#include "rssleaf.h"
#include "qrssglobals.h"
#include "httphandler.h"
#include "httpobserver.h"
#include "parser/httpparser.h"

//forward declaration
class QHttp;
class XmlParser;

class RssContainer : public QObject, public QXmlDefaultHandler, public HttpObserver
{
Q_OBJECT

public:
	enum RssState
	{
		EIDLE = 0,
		EXMLPARSING
	};

	RssContainer( QObject* parent );
	virtual ~RssContainer();
	
	void 		update();
	void 		addChannel( RssChannel& channel );
	void 		addChannel( QString& url );
	void		deleteChannel( int ind );
	void		readAll( int channelId );

	void 		getChannelFeeds( int ind );
	QUrl		getChannelUrl( int ind );
	QString 	getItemDescription( int channelId, int ind );
	void		setItemState( int channelId,int ind, ItemState state );
	QString		getChannelDescription( int ind );
	void		markForParse( int ind );
	void		parseLinks();

	void		storeData();
	void		loadData();

public: //from QXmlErrorHandler
	bool		error ( const QXmlParseException & exception );
	QString		errorString() const;
	bool		fatalError( const QXmlParseException & exception );
	bool		warning( const QXmlParseException & exception );

public: // from HttpObserver
	void		handleResponse( QByteArray data );
	void		error( int errCode ) ;
	void		requestFinished( bool error );
	void		httpDone();
	void		progress( int done, int total );
	void		httpResponseHeaderReceived( const QHttpResponseHeader & resp );

	private:
	void 		addCurChannel();
	QString		parseDate( QString date, QRssFType type );
	void		refreshCurrentChannel();
	void		timerEvent( QTimerEvent *event );
	void		parseChannel( RssChannel& channel );
	void		initResource();
	QString		getResource( const QString& key, QRssFType& type);
	
//SLOTS
public slots:

	//from xmlParser
	void		documentEnd(RssChannel*);

	//from httpparser
	void		httpItemParserd(const QString& url);
	void		httpItemParsingError( int );

//SIGNALS
signals:
	void 		itemAdded( int, ItemState, const QString&, QString date );
	void 		channelAdded( const QString&, int, bool );
	void 		displayError( QString );

private:
	QList<RssChannel*>				m_channelsList;
	QList<RssChannel*>::iterator	m_channelsIterator;
	HttpHandler*					m_httpHandler;
	QXmlInputSource 				m_xmlInput;
	QXmlSimpleReader 				m_xmlReader;
	XmlParser* 						m_xmlParser;
	bool 							m_firstData;
	RssChannel* 					m_curChannel;
	QStringList						m_monthsName;
	bool							m_isUpdating;
	int								m_timerId;
	RssState						m_state;
	QStringList						m_hosts;
	HttpParser*						m_parser;
	QList<QString>					m_parsedLinks;
	QMap<QString, QPair<QString, QString> > m_resource;
};
#endif // RSSCONTAINER_H
//EOF

